<?php 
class adminControl extends control{
	function index(){
		if(!isset($_SESSION['user'])){
			echo "<script>window.location.href='admin.php'</script>";
			exit;
		}
		$this->display();
	}
	private function getCate(){
		$db=new mysql("category");
		$result=$db->select();
		$arr=array();
		$arr=$this->getCategory($result);
		return $arr;
	}
	function right(){
		$arr=$this->getCate();
		$str="<ul>";
		$str.=$this->getCateStr($arr);
		$str.="</ul>";
		$GLOBALS['cateStr']=$str;
		$this->display();
	}
	function getCategory($arr,$pid=0){
		$result=array();
		foreach($arr as $v){
			if($v['pid']==$pid){
				$result[$v['id']]=$v;
				$result[$v['id']]['sub']=$this->getCategory($arr,$v['id']);
				if(empty($result[$v['id']]['sub'])){
					unset($result[$v['id']]['sub']);
				}
			}
		}
		return $result;
	}
	function getCateStr($arr){
		$str='';
		static $i=1;
		$c=str_repeat("&nbsp;",$i);
		foreach($arr as $v){
			if(isset($v['sub'])){
				if($v['pid']==0){
					$str.="<li class='parent'>{$v['cname']}(id=".$v['id'].")<ul>{$this->getCateStr($v['sub'])}</ul></li>";
				}else{
					$i+=3;
					$str.="<li>".$c."└─{$v['cname']}(id=".$v['id'].")<ul>{$this->getCateStr($v['sub'])}</ul></li>";
					$i-=3;
				}
			}else{
				if($v['pid']==0){
					$str.="<li class='parent'>{$v['cname']}(id=".$v['id'].")</li>";
				}else{
					$str.="<li>".$c."└─{$v['cname']}(id=".$v['id'].")</li>";
				}
			}
		}
		return $str;
	}
	function cateAdd(){
		$db=new mysql("category");
		$result=$db->select();
		$str="";
		$str.=$this->getOpt($result);
		$GLOBALS['cateOpt']=$str;
		$this->display();
	}
	private function getOpt($result,$pid=0){
		$str="";
		static $i=1;
		$c=str_repeat("&nbsp",$i);
		foreach($result as $v){
			if($v['pid']==$pid){
				$str.="<option value='".$v['id']."'>".$c."└─".$v['cname']."</option>";
				$i+=2;
				$str.=$this->getOpt($result,$v['id']);
			}
		}
		$i-=2;
		return $str;
	}
	public function cateAddDeal(){
		$db=new mysql("category");
		$db->where("cname='".$_POST['cname']."' and pid=".$_POST['pid'])->select();
		$num=$db->affected_rows;
		$arr=array();
		if($num>0){
			$arr['state']=0;
			echo json_encode($arr);
			exit;
		}else{
			$db->insert();
			$arr['state']=1;
			echo json_encode($arr);
		}
	}
	public function cateMod(){
		$db=new mysql("category");
		$result=$db->select();
		$str="";
		$str.=$this->getOpt($result);
		$GLOBALS['cateOpt']=$str;
		$this->display();
	}
	public function cateModDeal(){
		$db=new mysql("category");
		//验证当前目录下目录名是否存在
		if(isset($_POST['cname']) && !isset($_POST['pid'])){
			$result=$db->where("id=".$_POST['id'])->select();
			$pid=$result[0]['pid'];
			$db->where("cname='".$_POST['cname']."' and pid=".$pid)->select();
			$num=$db->affected_rows;
			if($num>0){
				echo 0;
				exit;
			}else{
				$db->update();
				echo 1;
				exit;
			}
		}
		//验证指定目录下目录名是否存在
		if(isset($_POST['pid']) && !isset($_POST['cname'])){
			$result=$db->where("id=".$_POST['id'])->select();
			$cname=$result[0]['cname'];
			$db->where("cname='".$cname."' and pid=".$_POST['pid'])->select();
			$num=$db->affected_rows;
			if($num>0){
				echo 0;
				exit;
			}else{
				$db->update();
				echo 1;
				exit;
			}
		}
		if(isset($_POST['cname']) && isset($_POST['pid'])){
			$result=$db->where("cname='".$_POST['cname']."' and pid=".$_POST['pid'])->select();
			$num=$db->affected_rows;
			if($num>0){
				echo 0;
				exit;
			}else{
				$db->update();
				echo 1;
				exit;
			}
		}
	}
	public function cateDel(){
		$db=new mysql("category");
		$result=$db->select();
		$str="";
		$str.=$this->getOpt($result);
		$GLOBALS['cateOpt']=$str;
		$this->display();
	}
	public function cateDelDeal(){
		$db=new mysql("category");
		$id=$_POST['id'];
		$arr=$db->where("id=".$id)->select();
		$pid=$arr[0]['pid'];
		if($_POST['type']==1){
			//只删除选中目录，子级目录自动升级
			$arr=$db->where("pid=".$id)->select();
			foreach($arr as $v){
				$db->update(array("id"=>$v['id'],"pid"=>$pid));
			}
			$db->del($id);
			echo "<script>top.location.href='admin?c=admin'</script>";
		}else{
			//删除选中目录，子级目录一同删除
			$this->del($id);
			echo "<script>top.location.href='admin.php?c=admin'</script>";
		}
	}
	public function del($id){
		static $db=null;
		if(is_null($db)){
			$db=new mysql("category");
			$db->del($id);
		}
		$arr=$db->where("pid=".$id)->select();
		if(!empty($arr)){
			foreach($arr as $v){
				$this->del($v['id']);
				$db->del($v['id']);
			}
		}
	}
	public function login(){
	echo "ok";
	}
}
?>